package evi.com.evi.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import evi.com.evi.R;
import evi.com.evi.application.MyApplication;
import evi.com.evi.utils.AppUtils;
import evi.com.evi.utils.ConnectUtil;
import evi.com.evi.utils.ContentUtils;
import evi.com.evi.utils.JSONUtils;
import evi.com.evi.utils.SPUtil;
import evi.com.evi.utils.ScreenUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 电话
 *
 * @author sunyu
 */

public class TelephoneActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "TAG";
    private ImageView tv_return_back;
    private EditText et_input;
    private TextView tv_save;

    private OkHttpClient client;
    private JSONUtils jsonUtils;

    private Dialog dialog;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtils.setTranStatus(this, false, Color.parseColor("#FF6600"));
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_telephone);
        initView();
    }

    private void initView() {
        tv_return_back = (ImageView) findViewById(R.id.tv_return_back);
        tv_return_back.setOnClickListener(this);

        et_input = (EditText) findViewById(R.id.et_input);
        tv_save = (TextView) findViewById(R.id.tv_save);

        client = new OkHttpClient();
        jsonUtils = new JSONUtils();

        intent = getIntent();
        String content = intent.getStringExtra("content");

        et_input.setText(content);

        tv_save.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_return_back:
                finish();
                break;

            case R.id.tv_save:        //保存
                updateInfo();
                break;
            default:
                break;
        }
    }

    private void updateInfo() {
        dialog = AppUtils.getLoadingDialog(this, "提示", "正在上传");
        dialog.show();
        String phoneNo = et_input.getText().toString();

        String token = SPUtil.getString(this, "token");

        FormBody.Builder builder = new FormBody.Builder();
        builder.add("phoneNo", phoneNo);

        RequestBody body = builder.build();


        Request request = new Request.Builder()
                .addHeader("token", token)
                .url(ContentUtils.updateMerchant_url)
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                toast("网络信号不佳，加载失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(ConnectUtil.isConnect(TelephoneActivity.this)){
                            toast("网络信号不佳，加载失败");
                        } else {
                            toast("网络未联接，请检查");
                        }
                    }
                });
                dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonData = response.body().string();
                if (jsonUtils.validate(jsonData)) {
                    JSONObject jsonObject = JSON.parseObject(jsonData);
                    if (jsonObject.getBoolean("success")) {
                        toast("修改成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    } else {
                        toast(jsonObject.getString("message"));
                    }
                }
                dismiss();
                Log.e(TAG, "更新商家联网成功" + jsonData);
            }
        });
    }

    private void toast(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(TelephoneActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void dismiss() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().deleteActivity(this);
    }
}
